package com.kunya.product.controller.portall;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.service.product.IProductImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 商品图片控制器
 * @author suimg
 * @date 2018/7/16 16:11
 */
@RestController
@RequestMapping("/product_image")
public class ProductImageController {

    @Autowired
    private IProductImageService iProductImageService;

    /**
     * 上传商品图片
     * @param file
     * @param productId
     * @return
     * @throws IOException
     */
    @RequestMapping(value="upload_image",method = RequestMethod.POST)
    public ServerResponse uploadImage(MultipartFile file,Long productId,@RequestParam(value = "sort", defaultValue = "0")Integer sort) throws IOException {
        return iProductImageService.uploadProductImage(file,productId,sort,"340_");
    }

    /**
     * 删除商品图片
     * @param productId
     * @param picImgId
     * @return
     */
    @RequestMapping(value = "remove_product_image",method = RequestMethod.GET)
    public ServerResponse removeProductImage(Long productId,Long picImgId) throws IOException {
       if(picImgId!=null){
           return iProductImageService.deleteProdudtImageById(picImgId,"340_");
       }else if(productId!=null){
           return iProductImageService.deleteProductImageByProductId(productId,"340_");
       }else{
           return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
       }
    }

    /**
     * 显示/隐藏 商品图片
     * @param picImgId
     * @return
     */
    @RequestMapping(value = "display_product_image",method = RequestMethod.GET)
    public ServerResponse displayProducyImage(Long picImgId){
        return iProductImageService.displayProductImage(picImgId,"340_");
    }
}
